package com.wh.springboot.neteasecloudapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wh.springboot.common.entity.ChinaTotal;
import com.wh.springboot.common.entity.CovidData;
import com.wh.springboot.service.ChinaTotalService;
import com.wh.springboot.service.CovidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务
 */
@Component
public class ChinaTotalScheduleTask {
    /**
     * 定义成每小时执行一次，更新全国数据情况
     *
     * @throws Exception
     */
    @Autowired
    private ChinaTotalService chinaTotalService;
    @Autowired
    private CovidService covidService;

    //    @Scheduled(fixedDelay = 3600000)//1小时执行一次
//    @Scheduled(fixedDelay = 100000)//100s执行一次
    @Scheduled(cron = "0 0 8,9,10,13,18,20 * * ?")
    public void updateChinaTotalToDataBase() throws Exception {
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
        System.out.println("原始数据:" + string);
        //1.所有数据的alibaba的格式
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");
        System.out.println("Data:" + data);
        //2.data类型的alibaba格式
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");
        System.out.println("ChinaTotal:" + chinaTotal);
        //3. total全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");
        System.out.println("Total：" + total);
        //4.全国数据total confirm确诊病例
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");

        //5. 为程序实体赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setInput((Integer) input);
        dataEntity.setSevere((Integer) severe);
        dataEntity.setHeal((Integer) heal);
        dataEntity.setDead((Integer) dead);
        dataEntity.setSuspect((Integer) suspect);
        //格式化时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dataEntity.setUpdateTime(format.parse(String.valueOf(lastUpdateTime)));

        //6. 插入数据库【更新】
        chinaTotalService.save(dataEntity);


        //拿到areaTree
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
//        System.out.println("AreaTree:" + areaTree);
        Object[] objects = areaTree.toArray();
        //遍历所有国家的数据
//        for (int i = 0; i < objects.length; i++) {
//            JSONObject jsonObjectArray = JSONObject.parseObject(objects[i].toString());
//            Object name = jsonObjectArray.get("name");
//            System.out.println("Name:" + name);
//
//        }

        //3.中国地区
        JSONObject jsonObjectChinaData = JSONObject.parseObject(objects[2].toString());
        JSONArray children = jsonObjectChinaData.getJSONArray("children");

        Object[] chinaprovinceobjects = children.toArray();

        SimpleDateFormat provinceformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<CovidData> list = new ArrayList<>();
        for (int i = 0; i < chinaprovinceobjects.length; i++) {
            CovidData covidData = new CovidData();
            JSONObject jsonObjectArray = JSONObject.parseObject(chinaprovinceobjects[i].toString());
            Object provincename = jsonObjectArray.get("name");//省份
            Object provincelastUpdateTime = jsonObjectArray.get("lastUpdateTime");//省份更新数据时间
            Object provincetotal = jsonObjectArray.get("total");

            JSONObject jsonObjectChinaProvinceData = JSONObject.parseObject(provincetotal.toString());//provincetotal
            Object provinceconfirm = jsonObjectChinaProvinceData.get("confirm");//省份累积确诊病例
            //现存确诊=累积确诊-死亡人数-治愈人数
            Object provinceheal = jsonObjectChinaProvinceData.get("heal");
            Object provincedead = jsonObjectChinaProvinceData.get("dead");

            //现存确诊
            int presentconfirmed=Integer.parseInt(provinceconfirm.toString())-Integer.parseInt(provinceheal.toString())-Integer.parseInt(provincedead.toString());

            System.out.println("省份->" + provincename + " 确诊人数:" + provinceconfirm);
            //赋值
            covidData.setName(provincename.toString());
//            covidData.setValue(Integer.parseInt(provinceconfirm.toString()));//转化为int类型
            covidData.setValue(presentconfirmed);//转化为int类型

            if (provincelastUpdateTime == null) {
                covidData.setUpdateTime(new Date());
            } else {
                covidData.setUpdateTime(provinceformat.parse(String.valueOf(provincelastUpdateTime)));
            }
            list.add(covidData);
        }

        //各个省份的数据插入到数据库当中
//        covidService.saveBatch(list);
        covidService.saveBatch(list);
    }


}
